from typing import List, Generic

from pydantic import BaseModel, Field, validator
from pydantic.generics import GenericModel

from apps.core.return_info import DataT

wfqk_headers = {
    'Accept': 'application/json, text/plain, */*',
    'Host': 'www.wanfangdata.com.cn', # c.wanfangdata.com.cn
    'Origin': 'http://www.wanfangdata.com.cn', # http://c.wanfangdata.com.cn
    'Referer': 'http://c.wanfangdata.com.cn/periodical',
    'Content-Type': 'application/json;charset=UTF-8',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0',
}


################# 下载model ##########################
class AZDownModel(BaseModel):
    pagenum: int = 1
    ch: str = ''
    class_code: str = ''
    marks: List[str] = []


class ClassModel(BaseModel):
    marks: List[str] = []
    item: str = ""


class WFQKDataRefModel(BaseModel):
    marks: List[str] = []
    ReferenceType: str = ''
    Id: str = ''
    PageNum: str = ''


class WFDownArticleModel(BaseModel):
    page: str = ''
    num: str = ''
    pubyear: str = ''
    pykm: str = ''
    marks: List[str] = []


################## 解析model #########################


################# 保存model ##########################
class SaveAZListModel(BaseModel):
    page: str = ""
    ch: str = ""
    html: str = ""


class SaveClassCodeModel(BaseModel):
    html: str = ""


class SaveClassLitteCodeModel(BaseModel):
    id_code: str = ""
    html: str = ""


class SaveRefModel(BaseModel):
    Id: str = ""
    page: str = ""
    count: str = ""
    html: str = ""


# 只有id和html的models
class WFQKInfoModel(BaseModel):
    pykm: str = ""
    information: str = ""
    synopsis: str = ""


class SaveIssueModel(BaseModel):
    pubyear: str
    num: str
    pykm: str
    page: str
    html: str


class SaveYearModel(BaseModel):
    pykm: str
    html: str = ""


class SaveRefMongoModel(BaseModel):
    id: str = Field("", alias="_id")
    count: str = ""
    page_info: dict = {}  # 步骤信息
    befor_times: dict = {}  # 上次备份
    update_time: str = ""
    create_time: str = ""

    @validator('*', pre=True)
    def deal_none(cls, v, field):
        if v is None or v == {} or v == "":
            return field.default
        return v

    @validator('update_time', 'create_time', pre=True)
    def split_str(cls, v):
        return str(v)
